from django.shortcuts import render
from web import models

def index(request):
    if request.method=='GET':
        #1表示用品 2表示食品
        commodity_list= {'name1':[],'name2':[],'stock1':[],'stock2':[],'buy1':[],'buy2':[],
                         'stars1':[],'stars2':[],'views1':[],'views2':[]}
        transaction_list={'transaction1':[],'transaction2':[]}
        my_list1 = models.Good.objects.filter()
        my_list2 = models.Transaction.objects.filter()
    for row in my_list1:
        # 判断是食品还是物品
        if row.type == 1:
            commodity_list['name1'].append(row.name)
            commodity_list['stock1'].append(row.stock)
            commodity_list['buy1'].append(row.buy)
            commodity_list['stars1'].append(row.stars)
            commodity_list['views1'].append(row.views)
        else:
            commodity_list['name2'].append(row.name)
            commodity_list['stock2'].append(row.stock)
            commodity_list['buy2'].append(row.buy)
            commodity_list['stars2'].append(row.stars)
            commodity_list['views2'].append(row.views)

    for row in my_list2:
        if row.type == 2:
            if row.good.type == 1:
                transaction_list['transaction1'] = [1, 5, 9, 21, 22, 24, 29]
            else:
                transaction_list['transaction2'] = [1, 7, 9, 15, 22, 30, 38]


    context = {
        'commodity_list': commodity_list,
        'transaction_list':transaction_list
    }
    return render(request, 'index.html', context)
